//
//  DestinationsViewController.m
//  chanyou
//
//  Created by wanli on 14/12/30.
//  Copyright (c) 2014年 wanli. All rights reserved.
//

//----------------显示目的地首页----------------------

#import "DestinationsViewController.h"
#import "ConnectModel.h"
#import "CityViewController.h"
#import "UIImageView+WebCache.h"
#import "SDImageCache.h"
@interface DestinationsViewController ()

@end

@implementation DestinationsViewController
- (void)dealloc
{

    [_array release];
    [_collArray release];
    [_name_zh_cn release];
    [_poi_count release];
    [_name_en release];
    [_coll release];
    [_countryArray release];
    [_cityArray release];
    [_name_en release];
    [_name_zh_cn release];
    [super dealloc];
}


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.collArray = [[NSMutableArray alloc] init];
        self.array = [[NSMutableArray alloc] init];
        self.cityArray = [[NSMutableArray alloc] init];

    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.countryArray = [NSMutableArray arrayWithObjects:@"外国.亚洲", @"国外.欧洲", @"美洲,大洋洲,非洲与南极洲", @"国内.港澳台", @"国内.大陆", nil];
    [self createCollectionView];
    [self startConnect];
    
    //
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(passAction:) name:@"PASSDATA" object:nil];
    

}
//jia
- (void)passAction:(NSNotification *)noti
{
    // if ([_name_zh_cn.text isEqualToString:[noti.userInfo objectForKey:@"name"]]) {
    NSLog(@"llllll = %@", [noti.userInfo objectForKey:@"name"]);
    
    for (int i = 0; i < [_collArray count]; i++) {
        
        NSMutableDictionary *dic = [_collArray objectAtIndex:i];
        NSMutableArray *array = [dic objectForKey:@"destinations"];
        for (int j = 0; j < [array count]; j++) {
            NSMutableDictionary *num = [array objectAtIndex:j];
            _name_zh_cn.text = [num objectForKey:@"name_zh_cn"];
            // 模糊查询
            NSRange range = [_name_zh_cn.text rangeOfString:[noti.userInfo objectForKey:@"name"]];
            if (range.length != 0) {
                _destination_id = [num objectForKey:@"id"];
                NSDictionary *passDic = [NSDictionary dictionaryWithObjectsAndKeys:_destination_id, @"ID", nil];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"PASSID" object:self userInfo:passDic];
            }
        }
    }
}



- (void)createCollectionView{
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.itemSize = CGSizeMake(self.view.frame.size.width / 2 - 15, self.view.frame.size.width / 2 + 40);
    flow.minimumInteritemSpacing = 0;
    flow.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    flow.minimumLineSpacing = 10;
    flow.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 40);
    
    self.coll = [[UICollectionView alloc]initWithFrame:CGRectMake(0, -35, self.view.frame.size.width, self.view.frame.size.height - 75) collectionViewLayout:flow];
    _coll.backgroundColor = [UIColor colorWithRed:246.0 /255 green:146.0 / 255 blue:246.0 / 255 alpha:0.4];
    
    _coll.delegate = self;
    _coll.dataSource = self;
    [_coll registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

//    [_coll registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
  

    _coll.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_coll];
    [flow release];
    [_coll release];
}

- (void)startConnect{
    __block DestinationsViewController *des = self;
    [ConnectModel connectModelWithUrl:@"http://chanyouji.com/api/destinations.json" parmaters:nil finished:^(NSData *data) {
        self.collArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        [des.coll reloadData];
    }];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
//    NSLog(@"num = %d", [_collArray count]);
    return [_collArray count];
}




- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
//    NSLog(@"section = %d", section);
    NSDictionary *dic = [_collArray objectAtIndex:section];
    _cityArray = [dic objectForKey:@"destinations"];
//    NSLog(@"city = %@", _cityArray);
    return [_cityArray count];
    
}




//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
//
//    
//    if (kind == UICollectionElementKindSectionHeader) {
//
//    
//    UICollectionReusableView *head = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
//        
//        
//    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
//    NSString *name = [_countryArray objectAtIndex:indexPath.section];
//        
//            
//            lab.text = name;
//            [head addSubview:lab];
//            [lab release];
//        lab.alpha = 0;
//
//
//        return head;}
//    else{
//        return nil;
//    }
//    
//}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    NSDictionary *d = [_collArray objectAtIndex:indexPath.section];
    _cityArray = [d objectForKey:@"destinations"];
    
    NSDictionary *dic = [_cityArray objectAtIndex:indexPath.item];
    NSString *path = [dic objectForKey:@"image_url"];
    
    UIImageView *image = [[UIImageView alloc]init];
//    [image imageWithUrl:path placeHolder:nil];
    [image sd_setImageWithURL:[NSURL URLWithString:path] placeholderImage:[UIImage imageNamed:@"placeHold.png"]];
    image.layer.cornerRadius = 3;
    image.layer.masksToBounds = YES;
    cell.backgroundView  = image;
    [image release];
    
    _name_zh_cn = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 200, 50)];
    _name_zh_cn.text = [dic objectForKey:@"name_zh_cn"];
    _name_zh_cn.font = [UIFont boldSystemFontOfSize:20];
    [_name_zh_cn setTextColor:[UIColor whiteColor]];
    _name_zh_cn.highlighted = YES;
    _name_zh_cn.shadowColor = [UIColor grayColor];
    //_citiName 要放在UIImageView上, 且UIImageView不是通过add 添加到cell, 而是通过cell.backgroundView 进行赋值, 这样,_cityName显示的内容才不会重复.
    [image addSubview:_name_zh_cn];
    [_name_zh_cn release];
    
    _name_en = [[UILabel alloc] initWithFrame:CGRectMake(10, 35, 200, 20)];
    _name_en.text = [dic objectForKey:@"name_en"];
    _name_en.font = [UIFont systemFontOfSize:18];
    [_name_en setTextColor:[UIColor whiteColor]];
    [image addSubview:_name_en];
    [_name_en release];
    
    _poi_count = [[UILabel alloc] initWithFrame:CGRectMake(image.frame.size.width / 3 - 10, image.frame.size.height - 40, 200, 40)];
    _poi_count.text = [NSString stringWithFormat:@"%@ %@", [dic objectForKey:@"poi_count"], @"旅行地"];
    _poi_count.font = [UIFont systemFontOfSize:15];
    [_poi_count setTextColor:[UIColor whiteColor]];
    _poi_count.shadowColor = [UIColor grayColor];
//    _poi_count.highlightedTextColor = [UIColor redColor];
    [image addSubview:_poi_count];
    [_poi_count release];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = [_collArray objectAtIndex:indexPath.section];
    NSArray *array = [dic objectForKey:@"destinations"];
    CityViewController *city = [[CityViewController alloc] init];
    NSDictionary *d = [array objectAtIndex:indexPath.row];
    city.ID = [NSString stringWithFormat:@"%@", [d objectForKey:@"id"]];
    [self.navigationController pushViewController:city animated:YES];
    [city release];
//    [[SDImageCache sharedImageCache] clearMemory];
}

- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated{
    scrollPosition = UICollectionViewScrollPositionTop;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
